package org.easy4j.framework.core.datasource.support;

import com.google.common.base.Strings;
import org.easy4j.framework.core.config.Config;
import org.easy4j.framework.core.config.DataSourceConfig;
import org.easy4j.framework.core.datasource.DataSourceFactory;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;


/**
 * @author bjliuyong
 * @version 1.0
 * @created date 16-6-17
 */
public class  H2DataSourceFacotory4UnitTest implements DataSourceFactory {

    /**
     * Get the {@link javax.sql.DataSource}
     */
    @Override
    public javax.sql.DataSource getDataSource() {

        EmbeddedDatabaseBuilder databaseBuilder = new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.H2);

        String sql = Config.getValue(DataSourceConfig.UNIT_TEST_SQL);

        if(!Strings.isNullOrEmpty(sql)){
            String[] arrSql = sql.split(",");
            databaseBuilder.addScripts(arrSql);
        }
        //ExportSchemaUtil.export(dataSource);
        return databaseBuilder.build() ;

    }



}
